package com.wyc.service.impl;

import com.wyc.aop.MyLog;
import com.wyc.mapper.DeptsMapper;
import com.wyc.mapper.EmpsMapper;
import com.wyc.pojo.Dept;
import com.wyc.pojo.Dept_log;
import com.wyc.service.Dept_logService;
import com.wyc.service.DeptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptsServiceImpl implements DeptsService {

    @Autowired
    private DeptsMapper deptsMapper;
    @Autowired
    private EmpsMapper empsMapper;

    @Autowired
    private Dept_logService dept_logService;

    @Override
    public List<Dept> selectAllDepts() {
        return deptsMapper.selectAllDepts();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Integer id) throws Exception {

        try {
            //删除部门
            deptsMapper.deleteById(id);

            int a=1/0;

//          if(true){
//              throw new Exception("啊实打实大大声大大大");
//          }
            //同时删除该部门对应的员工
            empsMapper.delByDeptId(id);
        }finally {
            Dept_log dept_log=new Dept_log();
            dept_log.setDes("执行了删除部门操作");
            dept_log.setCreateTime(LocalDateTime.now());
            dept_logService.save(dept_log);
        }


    }


    @MyLog
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptsMapper.add(dept);
    }
}
